package love.bot.forli.group.review.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import love.bot.forli.group.review.bean.Dto.TypeDto;
import love.bot.forli.group.review.bean.Result;
import love.bot.forli.group.review.bean.Vo.AddTypeVo;
import love.bot.forli.group.review.bean.Vo.TypeRuleVo;
import love.bot.forli.group.review.entity.Question;
import love.bot.forli.group.review.entity.QuestionGroup;
import love.bot.forli.group.review.entity.QuestionType;
import love.bot.forli.group.review.mapper.QuestionGroupMapper;
import love.bot.forli.group.review.mapper.QuestionMapper;
import love.bot.forli.group.review.mapper.QuestionTypeMapper;
import love.bot.forli.group.review.service.QuestionTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author Kontori
 */
@Service
public class QuestionTypeServiceImpl extends ServiceImpl<QuestionTypeMapper, QuestionType> implements QuestionTypeService {

    private QuestionTypeMapper questionTypeMapper;

    @Autowired
    public void setQuestionTypeMapper(QuestionTypeMapper questionTypeMapper) {
        this.questionTypeMapper = questionTypeMapper;
    }

    private QuestionGroupMapper questionGroupMapper;

    @Autowired
    public void setQuestionGroupMapper(QuestionGroupMapper questionGroupMapper) {
        this.questionGroupMapper = questionGroupMapper;
    }

    private QuestionMapper questionMapper;

    @Autowired
    public void setQuestionMapper(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }

    @Override
    public Result<List<TypeDto>> listType() {
        List<TypeDto> typeDtoList = new ArrayList<>();
        questionTypeMapper.selectList(null).forEach(questionType -> {
            TypeDto typeDto = new TypeDto();
            BeanUtils.copyProperties(questionType, typeDto);
            AtomicReference<Long> questionTotal = new AtomicReference<>(0L);
            questionGroupMapper.selectList(new QueryWrapper<QuestionGroup>()
                    .eq("question_type_id",questionType.getQuestionTypeId())).forEach(questionGroup ->
                    questionTotal.updateAndGet(v -> v +
                            questionMapper.selectCount(new QueryWrapper<Question>()
                                    .eq("question_group_id",questionGroup.getQuestionGroupId()))));
            typeDto.setQuestionTotal(questionTotal.get());
            typeDtoList.add(typeDto);
        });
        return Result.success(typeDtoList);
    }

    @Override
    public Result<String> updateRule(TypeRuleVo typeRuleVo) {
        typeRuleVo.getRules().forEach(typeRule -> questionTypeMapper.update(null,new UpdateWrapper<QuestionType>()
                .eq("question_type_id",typeRule.getQuestionTypeId())
                .set("need_count",typeRule.getNeedCount())));
        return Result.success("更新成功",null);
    }

    @Override
    public Result<Object> deleteType(Integer questionTypeId) {
        QuestionType questionType = questionTypeMapper.selectById(questionTypeId);
        if (questionType == null) {
            return Result.fail("对应题组类型不存在");
        }
        //高危操作，若题目数量不为空，不允许直接删除
        if (questionGroupMapper.selectCount(new QueryWrapper<QuestionGroup>()
                .eq("question_type_id", questionTypeId)) != 0) {
            return Result.fail("该类型下存在多个题组，不允许删除");
        }
        questionTypeMapper.deleteById(questionType);
        return Result.success("删除成功", null);
    }

    @Override
    public Result<Object> addType(AddTypeVo addTypeVo) {
        if (questionTypeMapper.selectOne(new QueryWrapper<QuestionType>()
                .eq("type_name", addTypeVo.getName())) != null) {
            return Result.fail("题组类型名称重复");
        }
        QuestionType questionType = new QuestionType();
        questionType.setTypeName(addTypeVo.getName());
        questionType.setNeedCount(addTypeVo.getNeedCount());
        questionTypeMapper.insert(questionType);
        return Result.success("新增成功", null);
    }
}
